package com.yinp.proapp.module.qq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.Tencent;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.databinding.ActivityQQLoginBinding;
import com.yinp.proapp.module.qq.BaseApiListener;
import com.yinp.proapp.module.qq.BaseUiListener;
import com.yinp.proapp.module.qq.QQLoginBean;
import com.yinp.proapp.utils.GlideUtils;
import com.yinp.proapp.utils.StatusBarUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @Author: yinp
 * @Date: 2021/8/23
 * @Description:  还是用极光的吧
 */
public class QQLoginActivity extends AppBaseActivity<ActivityQQLoginBinding> {
    Tencent mTencent;
    private static final String appId = "1108225289";
    BaseUiListener baseUiListener = new BaseUiListener() {
        @Override
        public void onComplete(Object o) {
            bd.tvLogin.setText("已登录");
            Log.d("abcd", "onComplete: " + o.toString());
            QQLoginBean qqLoginBean = new Gson().fromJson(o.toString(), QQLoginBean.class);
            Bundle bundle = new Bundle();
            bundle.putString("format", "json");
            bundle.putString("status_os", android.os.Build.VERSION.RELEASE);
            bundle.putString("status_machine", android.os.Build.MODEL);
            bundle.putString("sdkv", Constants.SDK_VERSION);
            bundle.putString("status_version", android.os.Build.VERSION.SDK);
            bundle.putString("sdkp", "a");

//            if (mTencent.getQQToken() != null && mTencent.getQQToken().isSessionValid()) {
            bundle.putString(Constants.PARAM_CONSUMER_KEY, appId);
            bundle.putString(Constants.PARAM_ACCESS_TOKEN, qqLoginBean.getAccess_token());
            bundle.putString(Constants.PARAM_OPEN_ID, qqLoginBean.getOpenid());
            bundle.putString("appid_for_getting_config", appId);
//            }
            GlideUtils.into(mContext, "http://thirdqq.qlogo.cn/g?b=oidb&k=w33Ce1eYV0ic4NUZVanYWTw&s=100&t=1617760135", bd.ivQqHead);

            bundle.putString(Constants.PARAM_PLATFORM_ID, qqLoginBean.getPf());
            mTencent.requestAsync("https://graph.qq.com/user/get_user_info", bundle, Constants.HTTP_GET, new BaseApiListener() {
                @Override
                public void onComplete(JSONObject jsonObject) {
                    Log.d(TAG, "onComplete:ss " + jsonObject);
                    try {
                        String figureurl_qq_2 = jsonObject.getString("figureurl");
//                        figureurl_qq_2 = "http://thirdqq.qlogo.cn/g?b=oidb&k=w33Ce1eYV0ic4NUZVanYWTw&s=100&t=1617760135";
                        //此处不能加载图片，会不显示
//                        GlideUtils.into(mContext,figureurl_qq_2,bd.ivQqHead);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    };

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.header.headerBackImg, bd.tvLogin);
        bd.header.headerCenterTitle.setText("qq的一些操作");
        mTencent = Tencent.createInstance(appId, this.getApplicationContext(), "Authorities");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {

        } else if (v == bd.tvLogin) {
            if (!mTencent.isSessionValid()) {
                mTencent.login(this, "all", baseUiListener);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, baseUiListener);
        if (requestCode == Constants.REQUEST_LOGIN) {
            Tencent.handleResultData(data, baseUiListener);
        }
    }
}